package com.znjc.web.controller.device;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
import javax.servlet.http.HttpServletResponse;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.znjc.common.core.domain.entity.SysUser;
import com.znjc.device.domain.*;
import com.znjc.device.service.IIotDataCheckService;
import com.znjc.device.service.IIotSerialService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.znjc.common.annotation.Log;
import com.znjc.common.core.controller.BaseController;
import com.znjc.common.core.domain.AjaxResult;
import com.znjc.common.enums.BusinessType;
import com.znjc.device.service.IIotSerialCheckRefService;
import com.znjc.common.utils.poi.ExcelUtil;
import com.znjc.common.core.page.TableDataInfo;

/**
 * 【串口与校验】Controller
 * 
 * @author zengpc
 * @date 2023-12-04
 */
@RestController
@RequestMapping("/device/serialCheckRef")
public class IotSerialCheckRefController extends BaseController
{
    @Autowired
    private IIotSerialCheckRefService iotSerialCheckRefService;
    @Autowired
    private IIotSerialService iotSerialService;
    @Autowired
    private IIotDataCheckService iotDataCheckService;

    /**
     * 根据串口id获取授权串口校验
     */
    @PreAuthorize("@ss.hasPermi('device:serialCheckRef:edit')")
    @GetMapping("/authDataCheck/{serialId}")
    public AjaxResult authDataCheck(@PathVariable("serialId") Long serialId) {
        AjaxResult ajax = AjaxResult.success();
        IotSerial serial = iotSerialService.getById(serialId);
        ajax.put("serial", serial);
        List<IotDataCheck> dataChecks = iotDataCheckService.list();
        if (CollectionUtils.isNotEmpty(dataChecks)) {
            List<Long> dataCheckIds = iotSerialCheckRefService.getIdList(serialId, 1);
            if (CollectionUtils.isNotEmpty(dataCheckIds)) {
                dataChecks.stream().map(d -> {
                    if (dataCheckIds.contains(d.getId())) {
                        d.setFlag(true);
                    }
                    return d;
                }).collect(Collectors.toList());
            }
            ajax.put("dataChecks", SysUser.isAdmin(serialId) ? dataChecks : dataChecks.stream().filter(r -> !r.isAdmin()).collect(Collectors.toList()));
        }
        return ajax;
    }

    /**
     * 串口绑定串口校验
     */
    @PreAuthorize("@ss.hasPermi('device:serialCheckRef:edit')")
    @Log(title = "串口校验绑定串口管理", businessType = BusinessType.GRANT)
    @PutMapping("/authDataCheck")
    public AjaxResult insertAuthRole(Long serialId, Long[] dataCheckIds) {
        if (serialId != null && dataCheckIds.length == 0) {
            LambdaQueryWrapper<IotSerialCheckRef> wrapper = new LambdaQueryWrapper<IotSerialCheckRef>();
            wrapper.eq(IotSerialCheckRef::getSerialId, serialId);
            iotSerialCheckRefService.remove(wrapper);
        }
        List<IotSerialCheckRef> refList = new ArrayList<IotSerialCheckRef>();
        if (serialId != null && dataCheckIds.length > 0) {
            IotSerialCheckRef ref = null;
            for (int i = 0; i < dataCheckIds.length; i++) {
                ref = new IotSerialCheckRef();
                ref.setCheckId(dataCheckIds[i]);
                ref.setSerialId(serialId);
                refList.add(ref);
            }
        }
        if (CollectionUtils.isNotEmpty(refList)) {
            //先删除
            LambdaQueryWrapper<IotSerialCheckRef> wrapper = new LambdaQueryWrapper<IotSerialCheckRef>();
            wrapper.eq(IotSerialCheckRef::getSerialId, serialId);
            iotSerialCheckRefService.remove(wrapper);
            //再保存
            iotSerialCheckRefService.saveBatch(refList);
        }
        return success();
    }
}
